package engine.scene
{
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.event.SceneEvent;
	import engine.flag.ResourcesPath;
	import engine.logic.loading.LoadingLogic;
	
	import flash.display.DisplayObject;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.loader.SwfLoader;
	import newx.utils.debug.IO;

	public class SceneRoleLoadLogic
	{
		private static var _self:SceneRoleLoadLogic;
		
		/***当前加载完成的个数***/
		private var loaded:int = 0;
		/**所有资源的总个数**/
		private var allResCount:int = 0;
		
		private var _model:ModelLocator;
		
		private var _cacheLists : Array = [];;
		public function SceneRoleLoadLogic()
		{
			init();
		}
		
		public static function getInstance():SceneRoleLoadLogic
		{
			if(_self == null)
			{
				_self = new SceneRoleLoadLogic();
			}
			return _self
		}
		
		
		private function init():void
		{
			_model = ModelLocator.getInstance();	
		}
		
		/**
		 *加载角色资源
		 * @param e
		 * 
		 */
		public function loadRoleRes(loadArr:Array):void
		{
			allResCount = loadArr.length;
			
			var load : Boolean;
			loaded = 1;
			LoadingLogic.getInstance().setProcess(0,StaticIni.getSwfLangStr("UIStr_1001055"));
			var resProgressHandle:Function	=	function(e:*):void
			{
				if(e is ProgressEvent)
				{
					var process:Number = ((loaded-1)/allResCount)*100;
					var tp:Number	=	(e.bytesLoaded/e.bytesTotal) * process;
					LoadingLogic.getInstance().setProcess(process + ( 1 / allResCount ) * tp , StaticIni.getSwfLangStr ( "UIStr_1001055" ) );
				}
			};
			
			var loadedResHandle:Function = 
				function(val:DisplayObject,pList:Array):void
				{
					if(loaded == allResCount)
					{
						LoadingLogic.getInstance().setProcess(100,StaticIni.getSwfLangStr("UIStr_1001055"));
						LoadingLogic.getInstance().hide();
						_model.dispatchEvent(new SceneEvent(SceneEvent.onLoadRoleComplete,[]));
					}
					else
					{
						loaded++;
						LoadResList(loaded - 1);
					}
				}
			
			var LoadResList:Function = function(loadCpount:uint):void
			{
				SwfLoader.StartLoad(loadArr[loadCpount],null,loadedResHandle,[resProgressHandle,loadArr[loadCpount]]);
			}
			
			LoadResList(loaded - 1);
			
			
//			for(var i:int = 0;i < loadArr.length ; i ++)
//			{
//				if( _cacheLists.indexOf( loadArr[ i ] )  == -1 )
//				{
//					load = true;
//					
//					_cacheLists.push( loadArr[ i ] );
//					
//					SwfLoader.StartLoad( loadArr[i], null, onLoadRoleComplete, [resProgressHandle,loadArr.length, loadArr[i]] );
//				}else
//				{
//					allResCount--;
//				}
//			}
//			
//			if( load )
//			{
//				LoadingLogic.getInstance().setProcess(0,StaticIni.getSwfLangStr("加载角色资源中……"));
//			}else
//			{
//				_model.dispatchEvent(new SceneEvent(SceneEvent.onLoadRoleComplete,[]));
//			}
		}
		
//		public function resProgressHandle(e:*):void
//		{
//			if(e is ProgressEvent)
//			{
//				var process:Number = (curCount/allResCount);
//				
////				LoadingLogic.getInstance().setProcess( Number( process.toFixed ( 2 ) )  * 100 , StaticIni.getSwfLangStr("UIStr_1001055"));
//				
//				var tp:Number	=	(e.bytesLoaded/e.bytesTotal) * process;
//				LoadingLogic.getInstance().setProcess(100*tp,StaticIni.getSwfLangStr("UIStr_1001055"));
//				trace(100*tp,process,curCount,allResCount);
//			}
//			
//		}
		
//		/**
//		 *成功加载资源 
//		 * @param val
//		 * @param pList
//		 * 
//		 */
//		public function onLoadRoleComplete(val:DisplayObject,pList:Array):void
//		{
//			if(val == null)
//			{
//				IO.traceError("加载资源"+pList+"失败");
////				return;
//			}
//			curCount ++;
//			trace(curCount);
//			
//			if(curCount == allResCount)
//			{
//				LoadingLogic.getInstance().setProcess(100,StaticIni.getSwfLangStr("UIStr_1001055"));
//				LoadingLogic.getInstance().hide();
//				_model.dispatchEvent(new SceneEvent(SceneEvent.onLoadRoleComplete,[]));
//				
//			}else
//			{
////				var process:Number = (curCount/allResCount);
////				
////				LoadingLogic.getInstance().setProcess( Number( process.toFixed ( 2 ) )  * 100 , StaticIni.getSwfLangStr("UIStr_1001055"));
//			}
//		}
	}
}